package com.kuaimacode.kframework.admin.controller.content;


import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.content.ContentService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.enums.ProtocolTypeEnum;
import com.kuaimacode.kframework.mybatis.models.index.Content;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/content")
public class ContentController {
    private static final String PAGE_FILE_NAME = "content/";

    @Autowired
    private ContentService contentService;



    @RequestMapping("/index")
    public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                        @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                        Content content,ModelMap model){
        PageInfo<Content> pageInfo = contentService.findAll(pageNum, pageSize,content);
        model.put("pageInfo", pageInfo);
        model.put("search", content);

        model.put("typeEnum", ProtocolTypeEnum.values());
        return PAGE_FILE_NAME + "index";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        Content content = contentService.getById(id);
        map.put("content", content);
        map.put("isAdd", false);
        map.put("typeEnum", ProtocolTypeEnum.values());
        return PAGE_FILE_NAME+"form";
    }

    @RequestMapping(value={"/edit"},method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Content content){


        try {
            contentService.updateContent(content);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();

    }
}
